1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
|
import dynamic from 'next/dynamic';
import NextImage from 'next/image';
import { useEffect, useState } from 'react';
import { useRouter } from 'next/router';
import Seo from '../../../core/components/Seo.jsx';
import Promocrumb from '../../../lib/promo/components/Promocrumb.jsx';
import LogoSpinner from '../../../core/components/elements/Spinner/LogoSpinner.jsx';
import ProductPromoCard from '../../../../src-migrate/modules/product-promo/components/Card.tsx';
import React from 'react';
import DesktopView from '../../../core/components/views/DesktopView.jsx';
import MobileView from '../../../core/components/views/MobileView.jsx';
import 'swiper/swiper-bundle.css';
import useDevice from '../../../core/hooks/useDevice.js';
import ProductFilterDesktop from '../../../lib/product/components/ProductFilterDesktopPromotion.jsx';
import ProductFilter from '../../../lib/product/components/ProductFilter.jsx';
import { HStack, Image, Tag, TagCloseButton, TagLabel } from '@chakra-ui/react';
import { formatCurrency } from '../../../core/utils/formatValue.js';
import Pagination from '../../../core/components/elements/Pagination/Pagination.js';
import whatsappUrl from '../../../core/utils/whatsappUrl.js';
import _ from 'lodash';
import useActive from '../../../core/hooks/useActive.js';
import useProductSearch from '../../../lib/promo/hooks/usePromotionSearch.js';
const BasicLayout = dynamic(() =>
import('../../../core/components/layouts/BasicLayout.jsx')
);
export default function PromoDetail() {
const router = useRouter();
const { slug = '', brand = '', category = '', page = '1' } = router.query;
const [currentPage, setCurrentPage] = useState(parseInt(10) || 1);
const [orderBy, setOrderBy] = useState(router.query?.orderBy);
const popup = useActive();
const prefixUrl = `/shop/promo/${slug}`;
const [queryFinal, setQueryFinal] = useState({});
const [limit, setLimit] = useState(30);
const [q, setQ] = useState('*');
const [finalQuery, setFinalQuery] = useState({});
const [products, setProducts] = useState(null);
const [brandValues, setBrand] = useState(
!router.pathname.includes('brands')
? router.query.brand
? router.query.brand.split(',')
: []
: []
);
const [categoryValues, setCategory] = useState(
router.query?.category?.split(',') || router.query?.category?.split(',')
);
const [priceFrom, setPriceFrom] = useState(router.query?.priceFrom || null);
const [priceTo, setPriceTo] = useState(router.query?.priceTo || null);
useEffect(() => {
const newQuery = {
fq: `type_value_s:${slug}`,
page: router.query.page ? router.query.page : 1,
brand: router.query.brand ? router.query.brand : '',
category: router.query.category ? router.query.category : '',
priceFrom: router.query.priceFrom ? router.query.priceFrom : '',
priceTo: router.query.priceTo ? router.query.priceTo : '',
limit: router.query.limit ? router.query.limit : '',
orderBy: router.query.orderBy ? router.query.orderBy : '',
};
setFinalQuery(newQuery);
}, [
router.query,
prefixUrl,
slug,
brand,
category,
priceFrom,
priceTo,
currentPage,
]);
useEffect(() => {
setQueryFinal({ ...finalQuery, q, limit, orderBy });
}, [
router.query,
prefixUrl,
slug,
brand,
category,
priceFrom,
priceTo,
currentPage,
finalQuery,
]);
const { productSearch } = useProductSearch({
query: queryFinal,
operation: 'OR',
});
const pageCount = Math.ceil(productSearch.data?.response.numFound / limit);
const productStart = productSearch.data?.responseHeader.params.start;
const productRows = limit;
const productFound = productSearch.data?.response.numFound;
useEffect(() => {
setProducts(productSearch.data?.response?.products);
}, [productSearch]);
const brands = [];
for (
let i = 0;
i <
productSearch.data?.facet_counts?.facet_fields?.manufacture_name_s.length;
i += 2
) {
const brand =
productSearch.data?.facet_counts?.facet_fields?.manufacture_name_s[i];
const qty =
productSearch.data?.facet_counts?.facet_fields?.manufacture_name_s[i + 1];
if (qty > 0) {
brands.push({ brand, qty });
}
}
const categories = [];
for (
let i = 0;
i < productSearch.data?.facet_counts?.facet_fields?.category_name.length;
i += 2
) {
const name =
productSearch.data?.facet_counts?.facet_fields?.category_name[i];
const qty =
productSearch.data?.facet_counts?.facet_fields?.category_name[i + 1];
if (qty > 0) {
categories.push({ name, qty });
}
}
function capitalizeFirstLetter(string) {
string = string.replace(/_/g, ' ');
return string.replace(/(^\w|\s\w)/g, function (match) {
return match.toUpperCase();
});
}
const handleDeleteFilter = async (source, value) => {
let params = {
q: router.query.q,
orderBy: '',
brand: brandValues.join(','),
category: categoryValues?.join(','),
priceFrom: priceFrom || '',
priceTo: priceTo || '',
};
let brands = brandValues;
let catagories = categoryValues;
switch (source) {
case 'brands':
brands = brandValues.filter((item) => item !== value);
params.brand = brands.join(',');
await setBrandValues(brands);
break;
case 'category':
catagories = categoryValues.filter((item) => item !== value);
params.category = catagories.join(',');
await setCategoryValues(catagories);
break;
case 'price':
params.priceFrom = '';
params.priceTo = '';
break;
case 'delete':
params = {
q: router.query.q,
orderBy: '',
brand: '',
category: '',
priceFrom: '',
priceTo: '',
};
break;
}
handleSubmitFilter(params);
};
const handleSubmitFilter = (params) => {
params = _.pickBy(params, _.identity);
params = toQuery(params);
router.push(`${slug}?${params}`);
};
const toQuery = (obj) => {
const str = Object.keys(obj)
.map(
(key) => `${encodeURIComponent(key)}=${encodeURIComponent(obj[key])}`
)
.join('&');
return str;
};
const whatPromo = capitalizeFirstLetter(slug);
const queryWithoutSlug = _.omit(router.query, ['slug']);
return (
<BasicLayout>
<Seo
title={`Promo ${Array.isArray(slug) ? slug[0] : slug} Terkini`}
description='B2B Marketplace MRO & Industri dengan Layanan Pembayaran Tempo, Faktur Pajak, Online Quotation, Garansi Resmi & Harga Kompetitif'
canonical={`${process.env.NEXT_PUBLIC_SELF_HOST}${
router.asPath.split('?')[0]
}`}
/>
<Promocrumb brandName={whatPromo} />
<MobileView>
<div className='p-4 pt-0'>
<h1 className='mb-2 font-semibold text-h-sm'>Promo {whatPromo}</h1>
<FilterChoicesComponent
brandValues={brandValues}
categoryValues={categoryValues}
priceFrom={priceFrom}
priceTo={priceTo}
handleDeleteFilter={handleDeleteFilter}
/>
{products?.length >= 1 && (
<div className='flex items-center gap-x-2 mb-5 justify-between'>
<div>
<button
className='btn-light py-2 px-5 h-[40px]'
onClick={popup.activate}
>
Filter
</button>
</div>
</div>
)}
{productSearch.isLoading && (
<div className='container flex justify-center my-4'>
<LogoSpinner width={48} height={48} />
</div>
)}
{products && (
<>
<div className='grid grid-cols-1 gap-x-1 gap-y-1'>
{products?.map((promotion) => (
<div
key={promotion.id}
className='min-w-36 max-w-[400px] mb-[20px] sm:w-full md:w-1/2 lg:w-1/3 xl:w-1/4 '
>
<ProductPromoCard promotion={promotion} />
</div>
))}
</div>
</>
)}
<Pagination
pageCount={pageCount}
currentPage={parseInt(page)}
url={`${prefixUrl}?${toQuery(_.omit(queryWithoutSlug, ['page']))}`}
className='mt-6 mb-2'
/>
<ProductFilter
active={popup.active}
close={popup.deactivate}
brands={brands || []}
categories={categories || []}
prefixUrl={
router.asPath.includes('?')
? `${router.asPath}`
: `${router.asPath}?`
}
defaultBrand={null}
/>
</div>
</MobileView>
<DesktopView>
<div className='container mx-auto flex mb-3 flex-col'>
<div className='w-full pl-6'>
<h1 className='text-2xl mb-2 font-semibold'>Promo {whatPromo}</h1>
<div className=' w-full h-full flex flex-row items-center '>
<div className='detail-filter w-1/2 flex justify-start items-center mt-4'>
<FilterChoicesComponent
brandValues={brandValues}
categoryValues={categoryValues}
priceFrom={priceFrom}
priceTo={priceTo}
handleDeleteFilter={handleDeleteFilter}
/>
</div>
<div className='Filter w-1/2 flex flex-col'>
<ProductFilterDesktop
brands={brands || []}
categories={categories || []}
prefixUrl={'/shop/promo'}
// defaultBrand={null}
/>
</div>
</div>
{productSearch.isLoading ? (
<div className='container flex justify-center my-4'>
<LogoSpinner width={48} height={48} />
</div>
) : products && products.length >= 1 ? (
<>
<div className='grid grid-cols-1 gap-x-6 sm:grid-cols-1 md:grid-cols-2 lg:grid-cols-2 xl:grid-cols-3'>
{products?.map((promotion) => (
<div
key={promotion.id}
className='min-w-[400px] max-w-[400px] mb-[20px] sm:min-w-[350px] md:min-w-[380px] lg:min-w-[400px] xl:min-w-[400px] '
>
<ProductPromoCard promotion={promotion} />
</div>
))}
</div>
</>
) : (
<div className='text-center my-8'>
<p>Belum ada promo pada kategori ini</p>
</div>
)}
<div className='flex justify-between items-center mt-6 mb-2'>
<div className='pt-2 pb-6 flex items-center gap-x-3'>
<NextImage
src='/images/logo-question.png'
alt='Logo Question Indoteknik'
width={60}
height={60}
/>
<div className='text-gray_r-12/90'>
<span>
Barang yang anda cari tidak ada?{' '}
<a
href={
router.query?.q
? whatsappUrl('productSearch', {
name: router.query.q,
})
: whatsappUrl()
}
className='text-danger-500'
>
Hubungi Kami
</a>
</span>
</div>
</div>
<Pagination
pageCount={pageCount}
currentPage={parseInt(page)}
url={`${prefixUrl}?${toQuery(
_.omit(queryWithoutSlug, ['page'])
)}`}
className='mt-6 mb-2'
/>
</div>
</div>
</div>
</DesktopView>
</BasicLayout>
);
}
const FilterChoicesComponent = ({
brandValues,
categoryValues,
priceFrom,
priceTo,
handleDeleteFilter,
}) => (
<div className='flex items-center mb-4'>
<HStack spacing={2} className='flex-wrap'>
{brandValues?.map((value, index) => (
<Tag
size='lg'
key={index}
borderRadius='lg'
variant='outline'
colorScheme='gray'
>
<TagLabel>{value}</TagLabel>
<TagCloseButton onClick={() => handleDeleteFilter('brands', value)} />
</Tag>
))}
{categoryValues?.map((value, index) => (
<Tag
size='lg'
key={index}
borderRadius='lg'
variant='outline'
colorScheme='gray'
>
<TagLabel>{value}</TagLabel>
<TagCloseButton
onClick={() => handleDeleteFilter('category', value)}
/>
</Tag>
))}
{priceFrom && priceTo && (
<Tag size='lg' borderRadius='lg' variant='outline' colorScheme='gray'>
<TagLabel>
{formatCurrency(priceFrom) + '-' + formatCurrency(priceTo)}
</TagLabel>
<TagCloseButton
onClick={() => handleDeleteFilter('price', priceFrom)}
/>
</Tag>
)}
{brandValues?.length > 0 ||
categoryValues?.length > 0 ||
priceFrom ||
priceTo ? (
<span>
<button
className='btn-transparent py-2 px-5 h-[40px] text-red-700'
onClick={() => handleDeleteFilter('delete')}
>
Hapus Semua
</button>
</span>
) : (
''
)}
</HStack>
</div>
);
|